package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

// CreateReqMessage 创建ReqMessage记录
// Author [piexlmax](https://github.com/piexlmax)
func CreateReqMessage(msg *model.ReqMessage) (err error) {
	err = global.GVA_DB.Create(msg).Error
	return err
}

// DeleteReqMessage 删除ReqMessage记录
// Author [piexlmax](https://github.com/piexlmax)
func DeleteReqMessage(msg model.ReqMessage) (err error) {
	err = global.GVA_DB.Delete(&msg).Error
	return err
}

// DeleteReqMessageByIds 批量删除ReqMessage记录
// Author [piexlmax](https://github.com/piexlmax)
func DeleteReqMessageByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.ReqMessage{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateReqMessage 更新ReqMessage记录
// Author [piexlmax](https://github.com/piexlmax)
func UpdateReqMessage(msg model.ReqMessage) (err error) {
	err = global.GVA_DB.Save(&msg).Error
	return err
}

// GetReqMessage 根据id获取ReqMessage记录
// Author [piexlmax](https://github.com/piexlmax)
func GetReqMessage(id uint) (err error, msg model.ReqMessage) {
	err = global.GVA_DB.Where("id = ?", id).First(&msg).Error
	return
}

// GetReqMessageInfoList 分页获取ReqMessage记录
// Author [piexlmax](https://github.com/piexlmax)
func GetReqMessageInfoList(info request.ReqMessageSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.ReqMessage{})
	var msgs []model.ReqMessage
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.ReqName != "" {
		db = db.Where("`req_name` LIKE ?", "%"+info.ReqName+"%")
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&msgs).Error
	return err, msgs, total
}
